package cn.ly.net.udp;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

/**
 * udp洗衣发送数据
 * a：创建发送端的socket对象
 * b：创建数据并把数据打包
 * c：调用socket对象的发送方法发送数据
 * d：释放资源
 * @author liuyu
 *
 */

public class SendDemo {
	public static void main(String[] args) throws Exception {
		//创建udp socket对象
		DatagramSocket ds = new DatagramSocket();
		
		//创建数据，并打包
		//DatagramPacket(byte[] buf, int length, InetAddress address, int port) 
		//创建数据
		byte[] bys = "hello udp,我来了".getBytes();//不要超过1472
		//长度
		int length = bys.length;
		//ip地址对象
		InetAddress address = InetAddress.getByName("192.168.0.103");
		//端口
		int port = 10086;
		DatagramPacket dp = new DatagramPacket(bys,length,address,port);
		
		//调用socket对象的发送方法发送数据包
		//public void send(DatagramPacket p)
		ds.send(dp);
		
		//释放资源
		ds.close();
	}
}
